<?php $edit_layout = ($this->Membership_model->is_owner($bricabox->id)
						&& strpos($this->uri->uri_string(), 'edit_layout') !== false) ? true : false; ?>

<?php
if ($this->Membership_model->is_owner($bricabox->id)):
	if ($edit_layout):
?>
<div id="edit_layout">
	<a class="on" href="/<?php if (strpos($this->uri->uri_string(), 'content') !== false) echo "content/{$content->id}"; ?>">I'm finished editing this page's layout</a>
</div>
<?php
	elseif (false):
?>
<div id="edit_layout">
	<a href="<?php if ($this->uri->uri_string() != "") echo '/'.$this->uri->uri_string(); ?>/edit_layout">Edit the layout of this page</a>
</div>
<?php
	endif;
endif;
?>

<?php if ($edit_layout): ?>
<div class="clearfix"></div>
<div id="block_catalog">
	<?php foreach ($avail_blocks->result() as $avail_block): ?>
	<div id="add_block_<?php echo $avail_block->id; ?>" class="add_block">
		<?php echo $avail_block->title; ?>
	</div>
	
	<script type="text/javascript" charset="utf-8">
		$('#add_block_<?php echo $avail_block->id; ?>').Draggable({
			revert: 	true,
			floats:		true,
			ghosting: 	true
		});
	</script>
	<?php endforeach; ?>
</div>
<?php endif; ?>

<?php
// render blocks
$cts = $this->Bricabox_model->get_content_types($bricabox->id);
foreach (array('large', 'small') as $size)
{
	echo "<div class=\"column {$location} edit_{$edit_layout}\" id=\"blocks_{$size}\">\n";
	
	if ($edit_layout)
	{
		echo "<div class=\"drop_locale\" id=\"blocks_{$size}_drop\">Drop blocks here to add them below.</div>\n";
		?>
		<style type="text/css" media="screen">
			.block_instance { text-align: left !important; }
		</style>
		<?php
	}
	
	if (count($blocks[$size]))
	{
		foreach ($blocks[$size] as $i => $block_data)
		{
			$method				 = "{$size}_{$block_data['data']->location}";
			$block_data['klass'] = BlockInstance::load_block_file(
				$block_data['template_path'],
				$block_data['parent']->name
			);
			$block_data['title'] = BlockInstance::title_for_block($block_data);
			
			echo "\t<!-- name: {$block_data['parent']->name} instance_id: {$block_data['data']->id} -->\n";
			echo "\t<div class=\"block_instance\" id=\"block_instance_{$block_data['data']->id}\">\n";
			if (!$edit_layout)
			{
				$has_prefs_var 	= "has_prefs_{$location}";
				$pref_link 		= ($this->Membership_model->is_owner($bricabox->id)
									&& $block_data['parent']->$has_prefs_var)
									? ' <a
										class="toggle toggle_prefs"
										href="#"
										onclick="return PrefPane.toggle('.$block_data['data']->id.');"
										>Edit</a>'
									: '';
				echo "\t\t<h3>{$block_data['title']}{$pref_link}</h3>\n";
				$block_data['klass']->$method($block_data, $content);
			}
			else
			{
				// draggables for editing the layout
				echo "\t\t<h3><span class=\"title\">{$block_data['title']}</span>&nbsp;<span class=\"dragme\">Drag this to reorder</span></h3>\n";
?>
<div class="edit_block_links">
	<a href="#" onclick="return editTitle(<?php echo $block_data['data']->id; ?>);">Edit this block's title</a>
	or <a href="#" onclick="return removeBlock(<?php echo $block_data['data']->id; ?>);"><img src="/assets/images/icons/cross.png" alt="X" />
		Remove from layout</a>
	<?php
	if ($location == 'content')
	{
		echo "<br />
<h3>Show this block for these kinds of entries:</h3>\n";
		foreach ($cts->result() as $ct)
		{
			$field_id = "exclude_{$block_data['data']->id}_{$ct->id}";
?>
<input onclick="setExclusion(<?php echo $block_data['data']->id; ?>, <?php echo $ct->id; ?>);" type="checkbox" id="<?php echo $field_id; ?>" name="<?php echo $field_id; ?>" value="1" <?php if (!$this->Bricabox_model->block_excluded($block_data['data']->id, $ct->id)) echo ' checked="checked"'; ?> />
	<label for="<?php echo $field_id; ?>"><?php echo ucfirst(Inflect::pluralize($ct->name)); ?></label><br />
<?php
		}
	}
	?>
</div>
<?php
			}
			echo "\n\t</div>\n\n";
		}
	}
	if ($edit_layout):
	?>
<script type="text/javascript" charset="utf-8">
$(document).ready(
	function ()
	{
		$('#blocks_<?php echo $size; ?>').Sortable({
			accept: 	'block_instance',
			helperclass: 'drophelper',
			tolerance: 	'intersect',
			handle: 	'.dragme',
			floats: 	true,
			onStop: 	function(ser)
			{
				var sorted_lg = $.SortSerialize('blocks_large');
				var sorted_sm = $.SortSerialize('blocks_small');
				var ids_lg	  = sorted_lg.hash.replace(/block_instance_/g, '').replace(/blocks_(large|small)\[\]\=/g, '').split('&').join('~');
				var ids_sm	  = sorted_sm.hash.replace(/block_instance_/g, '').replace(/blocks_(large|small)\[\]\=/g, '').split('&').join('~');
				$.post('/bricabox/<?php echo $bricabox->id; ?>/sort_blocks', {location: '<?php echo $location; ?>', size: 'large', ids: ids_lg});
				$.post('/bricabox/<?php echo $bricabox->id; ?>/sort_blocks', {location: '<?php echo $location; ?>', size: 'small', ids: ids_sm});
			}
		})
		$('#blocks_<?php echo $size; ?>_drop').Droppable({
			accept: 	'add_block',
			hoverclass: 'dropover',
			tolerance: 	'intersect',
			floats: 	true,
			onDrop: 	function(drag)
			{
				// steps to take:
				// 1. $.post the addition and get back some html
				// 2. append() the html to our sortable for this col
				// 3. call SortableAddItem on it
				// 4. done!
				
				var size_mode 	= this.id.match(/blocks_(large|small)_drop/);
				size_mode 		= size_mode[1];
				block_id		= drag.id.replace(/add_block_/, '');
				
				$.post('/bricabox/<?php echo $bricabox->id; ?>/add_block',
					{
						location: '<?php echo $location; ?>',
						size: size_mode,
						block_id: block_id
					}, function(data, textStatus)
				{
					var matched_id = data.match(/(block_instance_[\d]+)/);
					$('#blocks_'+size_mode).append(data).SortableAddItem(document.getElementById(matched_id[1]));
				});
			}
		});
	}
);
</script>
<?php
	endif;
	echo "\t<div class=\"block_footer\"></div>\n";
	echo "</div>\n";
}
?>

<?php if ($edit_layout): ?>
<script type="text/javascript" charset="utf-8">
	function editTitle(id)
	{
		var el	  = $('#block_instance_'+id+' h3 span.title');
		var title = el.attr('innerHTML');
		var new_t = prompt('Rename this block:', title);
		
		if (new_t != null)
		{
			// didn't hit cancel
			$.post('/bricabox/<?php echo $bricabox->id; ?>/save_title', {id: id, title: new_t});
			el.attr('innerHTML', new_t);
		}
		return false;
	}
	
	function removeBlock(id)
	{
		if (confirm('Are you sure you want to remove this block? You\'ll lose any custom settings for it. This cannot be undone.'))
		{
			$.post('/bricabox/<?php echo $bricabox->id; ?>/remove_block', {id: id});
			$('#block_instance_'+id).hide().remove();
		}
		return false;
	}
	
	function setExclusion(id, ct_id)
	{
		var el = $('#exclude_'+id+'_'+ct_id);
		if (el.attr('checked') == true)
			var val = "1";
		else
			var val = "0";
		
		$.post('/bricabox/<?php echo $bricabox->id; ?>/toggle_exclusion', {id: id, type_id: ct_id, value: val});
	}
</script>
<?php endif; ?>